/**
 * Auto gen via dbpojo-gen.
 */
package com.us.usws.dao;

import java.util.List;

import net.iwsh.core.db.DBExecutor;
import net.iwsh.core.db.ext.DialectSyntaxParser;
import net.iwsh.core.db.ext.SQLWithParams;
import net.iwsh.core.utils.IDGenerator;

import com.us.usws.model.domain.base.BaseDict;
import com.us.usws.model.flat.base.DOBaseDict;
import com.us.usws.support.uitls.DataPageUtil;

/**
 * DAO class
 * @author robot
 */
public class BaseDictDAO extends BasePageSearchDAO<BaseDict>{

	/**
	 * Query SQLs
	 */
	private static final String SQL_GET_TOTAL_COUNT = "select count(*) from hvb_dict";

	private static final String SQL_GET_ALL_BASEDICTS = "select * from hvb_dict order by domain,seq_num";

	private static final String SQL_GET_BASEDICT_BY_PK = "select * from hvb_dict where uid = ?";
	
	private static final String SQL_GET_BASEDICT_MAXSEPID = "select max(sep_id) from hvb_dict where domain = ?";

	/**
	 * CUR SQLs
	 */
	private static final String SQL_INSERT_BASEDICT = "insert into hvb_dict(uid,domain,code,name,seq_num,sep_id,extra_val,create_by_uid,create_date,last_upd_by_uid,last_upd_date) values (?,?,?,?,?,?,?,?,?,?,?)";

	private static final String SQL_UPDATE_BASEDICT = "update hvb_dict set domain=?, code=?, name=?, seq_num=?, sep_id=?, extra_val=?, last_upd_by_uid=?, last_upd_date=? where uid=?";

	private static final String SQL_DEL_BASEDICT = "delete from hvb_dict where uid=?";

	/**
	 * DBExeutor
	 */
	private DBExecutor dbe = new DBExecutor();

	/**
	 * Get Count.
	 */
	public int getTotalCount() {
		return dbe.getCount(SQL_GET_TOTAL_COUNT);
	}

	/**
	 * Get all list.
	 */
	public List<BaseDict> getAllBaseDicts() {
		return dbe.list(SQL_GET_ALL_BASEDICTS, BaseDict.class);
	}

	public void loadBaseDictsByPage(DataPageUtil<BaseDict> dataPageUtil) {
		String countSql = "select count(*) from hvb_dict where 1=1";
		String sql = " select * from hvb_dict where 1=1";
		SQLWithParams swp = initSQLWithParams(dataPageUtil, null, countSql, sql);
		dataPageUtil.setResult(dbe.list(swp.getSql(), BaseDict.class, swp.getParams()));
	}

	/**
	 * Get all list by pagination.
	 */
	public List<BaseDict> getBaseDicts(int start, int end) {
		SQLWithParams swp = DialectSyntaxParser.getParser().getPaginationSQLWithParams(SQL_GET_ALL_BASEDICTS, null, start, end);
		return dbe.list(swp.getSql(), BaseDict.class, swp.getParams());
	}

	/**
	 * Get BaseDict by primary key.
	 */
	public BaseDict getBaseDict(String uid) {
		BaseDict b = dbe.load(SQL_GET_BASEDICT_BY_PK, BaseDict.class, uid);
		return b;
	}

	/**
	 * Insert one row.
	 */
	public String insertBaseDict(DOBaseDict baseDict) {
		String uid = IDGenerator.getInstance().genUID();
		baseDict.setUid(uid);
		Integer sepid = baseDict.getSepId();
		if(sepid == null) {
			sepid = dbe.getCount(SQL_GET_BASEDICT_MAXSEPID, baseDict.getDomain());
			sepid++;
			baseDict.setSepId(sepid);
			baseDict.setSeqNum(sepid);
		}
		dbe.execute(SQL_INSERT_BASEDICT, baseDict.getUid(), baseDict.getDomain(), baseDict.getCode(), baseDict.getName(),
				baseDict.getSeqNum(), baseDict.getSepId(), baseDict.getExtraVal(), baseDict.getCreateByUid(), baseDict.getCreateDate(),
				baseDict.getLastUpdByUid(), baseDict.getLastUpdDate());
		return uid;
	}

	/**
	 * Update by PK.
	 */
	public void updateBaseDict(DOBaseDict baseDict) {
		dbe.execute(SQL_UPDATE_BASEDICT, baseDict.getDomain(), baseDict.getCode(), baseDict.getName(), baseDict.getSeqNum(),
				baseDict.getSepId(), baseDict.getExtraVal(), 
				baseDict.getLastUpdByUid(), baseDict.getLastUpdDate(), baseDict.getUid());
	}

	/**
	 * Delete row by PK.
	 */
	public void deleteBaseDict(String uid) {
		dbe.execute(SQL_DEL_BASEDICT, uid);
	}

}